package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.ErrorCode
import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ChargeProgressEntity
import com.hzjq.core.bean.ErrorReadCapEntity
import com.hzjq.core.bean.ErrorResult
import com.hzjq.core.callback.Callback
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work
import io.reactivex.functions.Consumer

class ChargeQueryWork : Work<ChargeProgressEntity> {

    private var mChargeErrorReadWork: ChargeErrorReadWork? = null
    private var errorCallback: Consumer<ErrorReadCapEntity>? = null

    constructor(
        callback: Callback<ChargeProgressEntity>?,
        errorCallback: Consumer<ErrorReadCapEntity>
    ) : super(callback) {
        this.errorCallback = errorCallback
    }

    override fun doWork(vararg args: Any) {
        val erc = object : ProgressCallback<ErrorReadCapEntity> {
            override fun onResult(t: ErrorReadCapEntity) {
                errorCallback?.accept(t)
            }

            override fun onRetryCountChanged(retryCount: Int, action: String) {
                callback?.onRetryCountChanged(retryCount, action)
            }

            override fun onError(errorCode: ErrorResult) {
                callback?.onError(errorCode)
            }

            override fun onProgressChanged(progress: Int, total: Int, action: String) {
                BlastDelegate.getDelegate().post(Runnable {
                    onProgressChanged(progress,  action)
                })
            }

        }
        val cpe = ChargeProgressEntity()
        Receives.getInstance()
            .registerReceiver(
                BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeCycleQueryCmd(),
                object : Receiver {
                    override fun convert(msg: String): Any {
                        return BlastDelegate.getDelegate().getParseLoader().parseScanProgress(msg)
                    }

                    override fun onSuccess(msg: Any) {
                        if (msg is CapProgressEntity) {
                            cpe.isEnd = false
                            cpe.mElectric = msg.mElectric
                            cpe.mVoltage = msg.mVoltage
                            cpe.progress = msg.progress
                            cpe.stateCode = msg.stateCode
                            callback?.onResult(cpe)

                            if (msg.mElectric == 0.0f && msg.mVoltage == 0.0f) {//充电异常
                                if(msg.stateCode == 3){
                                    onProgressChanged(100, "充电失败")
                                    callback?.onError(ErrorCode.getErrorResult(3))
                                    onDestroy()
                                } else {
                                    mChargeErrorReadWork = ChargeErrorReadWork(erc)
                                    mChargeErrorReadWork?.cancel()
                                    mChargeErrorReadWork?.doWork(msg.total)
                                }
                                return
                            }

                            if (msg.stateCode == 0 && msg.total <= BlastDelegate.getDelegate()
                                    .getMaxSupportCapCount()
                            ) {
                                if (msg.progress < 100) {
                                    onProgressChanged(msg.progress, "正在充电")
                                    retry()
                                } else {
                                    cpe.code = 0
                                    cpe.isEnd = true
                                    callback?.onResult(cpe)
                                    doNext()
                                }
                            } else {
                                if(msg.stateCode == 3){
                                    onProgressChanged(100, "充电失败")
                                    callback?.onError(ErrorCode.getErrorResult(3))
                                    onDestroy()
                                } else {
                                    mChargeErrorReadWork = ChargeErrorReadWork(erc)
                                    mChargeErrorReadWork?.cancel()
                                    mChargeErrorReadWork?.doWork(msg.total)
                                }
                            }
                        }

                    }

                    override fun failed() {
                        onProgressChanged(100, "充电失败")
                        callback?.onError(ErrorCode.getErrorResult(-7))
                        onDestroy()
                    }

                })

        val msg = DataMessageBean(
            BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeCycleQueryCmd().cmd
        )
        BlastDelegate.getDelegate().getCmdExeLoader().exeDelayPollResultCmd(
            msg.assembly(),
            BlastDelegate.getDelegate().getQueryDelayTime(),
            callback
        )
    }

    override fun cancel() {
        mChargeErrorReadWork?.cancel()
        Receives.getInstance()
            .unRegisterReceiver(
                BlastDelegate.getDelegate().getAssemblyCmdLoader().getChargeCycleQueryCmd()
            )
    }
}